package com.supper.customize.mytest;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yutao
 * @since 2020/4/15 3:47 下午
 */
public class MyJunitFrameWork {

    public static void main(String[] args) throws Exception {

        Class<EmployeeDaoTest> clazz = EmployeeDaoTest.class;

        EmployeeDaoTest employeeDaoTest = clazz.newInstance();

        Method[] methods = clazz.getMethods();
        List<Method> myBeforeList = new ArrayList<>();
        List<Method> myAfterList = new ArrayList<>();
        List<Method> myTestList = new ArrayList<>();

        for (Method method : methods) {
            if (method.isAnnotationPresent(MyBefore.class)) {
                myBeforeList.add(method);
            } else if (method.isAnnotationPresent(MyTest.class)) {
                myTestList.add(method);
            } else if (method.isAnnotationPresent(MyAfter.class)) {
                myAfterList.add(method);
            }
        }

        // 执行测试方法
        for (Method testMethod : myTestList) {
            // 执行@MyBefore的方法
            for (Method beforeMethod : myBeforeList) {
                beforeMethod.invoke(employeeDaoTest);
            }
            // 测试方法
            testMethod.invoke(employeeDaoTest);

            // 执行@MyAfter的方法
            for (Method afterMethod : myAfterList) {
                afterMethod.invoke(employeeDaoTest);
            }
        }
    }

}
